package br.unicamp.integralization.request;

import java.util.Collection;
import java.util.Map;

public class ValidationHelper {

	/**
	 * Checks if the string is empty or void.
	 * 
	 * @param string
	 *            the string
	 * @return true, if is empty or void
	 */
	public static boolean isEmptyOrVoid(final String string) {
		if (string == null || string.length() == 0) {
			return true;
		}
		return false;
	}

	/**
	 * Checks if the string is null or zero.
	 * 
	 * @param Integer
	 *            the integer
	 * @return true, if is null or zero
	 */
	public static boolean isNullOrZero(final Integer integer) {
		if (integer == null || integer.intValue() == 0) {
			return true;
		}
		return false;
	}

	/**
	 * Checks if the collection is empty or void.
	 * 
	 * @param list
	 *            the list
	 * @return true, if is empty or void
	 */
	public static boolean isEmptyOrVoid(Collection<?> list) {
		if (list == null || list.isEmpty()) {
			return true;
		}
		return false;
	}

	/**
	 * Checks if the map is empty or void.
	 * 
	 * @param list
	 *            the list
	 * @return true, if is empty or void
	 */
	public static boolean isEmptyOrVoid(Map<?, ?> list) {
		if (list == null || list.isEmpty()) {
			return true;
		}
		return false;
	}

	/**
	 * Checks if the validateString matches the regExp.
	 * 
	 * @param validateString
	 *            the validate string
	 * @param regExp
	 *            the reg exp to do the validation
	 * @return true, if is valid
	 */
	public static boolean isValid(String validateString, String regExp) {
		if (isEmptyOrVoid(validateString)) {
			return false;
		} else if (validateString.matches(regExp)) {
			return true;
		}
		return false;
	}

	/**
	 * Checks if it is a valid cpf number.
	 * 
	 * @param cpf
	 *            the cpf
	 * @return true, if is a valid cpf number
	 * 
	 * 
	 *         Author: Joao Gabriel Peres
	 */
	public static boolean isCpf(final String cpf) {

		if (isEmptyOrVoid(cpf)) {
			return false;
		}

		Integer i;
		String digit1;
		String digit2;
		final Long module = Long.valueOf(11);
		Long sum = Long.valueOf(0);
		Long rest;

		if (cpf.length() != 11) {
			return false;
		}

		// Calculates first digit
		Long c = Long.valueOf(10);
		for (i = 0; i < 9; i++) {
			sum += Long.valueOf(cpf.charAt(i) - '0') * c;
			c--;
		}
		rest = sum % module;
		if ((rest) < 2) {
			digit1 = "0";
		} else {
			digit1 = String.valueOf(module - rest);
		}

		// Calculates second digit
		sum = Long.valueOf(0);
		c = Long.valueOf(11);
		for (i = 0; i < 10; i++) {
			sum += Long.valueOf(cpf.charAt(i) - '0') * c;
			c--;
		}
		rest = sum % module;
		if ((rest) < 2) {
			digit2 = "0";
		} else {
			digit2 = String.valueOf(module - rest);
		}

		// Verifies digits
		if (cpf.charAt(9) != digit1.charAt(0)
				|| cpf.charAt(10) != digit2.charAt(0)) {
			return false;
		}

		return true;
	}

	public static boolean isValidCellPhone(String phoneAc, String phoneNb) {

		if (isEmptyOrVoid(phoneAc) || isEmptyOrVoid(phoneNb)) {
			return false;
		} else if (phoneAc.equals(ValidationConstants.PHONE_AREACODE_SP)) {
			if (phoneNb.matches(ValidationConstants.PHONENUMBER_CELL_SP)) {
				return true;
			}
		} else if (phoneNb.matches(ValidationConstants.PHONENUMBER_CELL)) {
			return true;
		}

		return false;
	}

	public static boolean isValidPhoneNumber(String phoneAc, String phoneNb) {

		if (isEmptyOrVoid(phoneAc) || isEmptyOrVoid(phoneNb)) {
			return false;
		} else if (phoneAc.equals(ValidationConstants.PHONE_AREACODE_SP)) {
			if (phoneNb.matches(ValidationConstants.PHONENUMBER_ANY_SP)) {
				return true;
			}
		} else if (phoneNb.matches(ValidationConstants.PHONENUMBER_ANY)) {
			return true;
		}

		return false;
	}
	
	/**
	 * Does the lunh card verification.
	 * 
	 * @param cardNumber
	 *            the card number
	 * @return true, if successful
	 */
	public static boolean lunhCardVerification(String cardNumber) {
		if (!isValid(cardNumber, ValidationConstants.NUMBER)) {
			return false;
		}

		int[] map = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, // for even indices
				0, 2, 4, 6, 8, 1, 3, 5, 7, 9 }; // for odd indices

		int sum = 0;
		int lastIndex = cardNumber.length() - 1;
		for (int i = 0; i <= lastIndex; i++) {
			sum += map[(cardNumber.charAt(lastIndex - i) - '0') + (i & 1) * 10];
		}
		return (sum % 10 == 0);
	}
}
